package com.tongtech.space.common.configs;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * swagger 配置
 *
 * @author jiayu
 * @since 2020/12/14
 */
@Configuration
@EnableOpenApi
//@EnableSwaggerBootstrapUI
//@Profile({"dev", "local"})
public class SwaggerConfig {

    /**
     * 存放Token的Header Key
     */
    public static final String HEADER_STRING = "Authorization";

    /**
     * 存放Cookie的Header Key
     */
    public static final String HEADER_COOKIE = "Cookie";


    @Value("${swagger.host}")
    private String swaggerHost;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .host(swaggerHost)
                .pathMapping("/")
                .groupName("API")
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api(扫描指定包中的swagger注解, 扫描所有)
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //.apis(RequestHandlerSelectors.basePackage("com.tongtech.space.controller"))
                //.apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesAdmin())
                .securityContexts(securityContextsAdmin())
                // 支持的通讯协议集合
                .protocols(newHashSet("https", "http"))
                // 扫描所有有注解的api，用这种方式更灵活
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 用来创建该API的基本信息，展示在文档的页面中（自定义展示的信息）
                .apiInfo(apiInfo());
    }

    /**
     * 认证的安全上下文
     */
    private List<SecurityScheme> securitySchemesAdmin() {
        return Arrays.asList(
                new ApiKey("请求头Cookie", HEADER_COOKIE, "header"));
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContextsAdmin() {
        return Arrays.asList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.any())
                        .build()
        );
    }

    /**
     * 支持的通讯协议集合
     *
     * @param type1
     * @param type2
     * @return
     */
    private Set<String> newHashSet(String type1, String type2) {
        Set<String> set = new HashSet<>();
        set.add(type1);
        set.add(type2);
        return set;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference(HEADER_COOKIE, authorizationScopes));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx 接口文档")
                .contact(new Contact("上海通办-研发二部-苏州研发中心","",""))
                .version("0.1")
                .termsOfServiceUrl("Terms of service")
                .description("swagger 电子文档")
                .license("License of API")
                .licenseUrl("API license URL")
                .build();
    }

}
